<?php

/**
 *
 * @return resource - Returns link to database on shared bluehost storage.
 * @author - JD Russell
 * @version - 1.0
 */

/* You may need to create the file 'db_access.php' Its content should look like 
<?php
$db_hostname = 'your_mysql_hostname';
$db_username = 'your_mysql_user';
$db_password = 'your_mysql_password';
$db_database = 'your_msql_database';
$db_salt = '%#$@!RandomString#%^123';
?>
*/ 
$db_cfg_file = "db_access.php";

require_once 'session.php';
function setup_page_redirect()
{
    // redirect to the setup page
    $wheriam = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'];
    $whereiwannabe = getcwd() . "/setup.php";
    if ($wheriam != $whereiwannabe)
    {
        echo "\n" . $wheriam . " != " .$whereiwannabe . "\n";
        getcwd() . "setup.php";
        header("Location: setup.php");
        exit();
    }
}

if (!include_once $db_cfg_file)
{
    setup_page_redirect();
}

function connect_db()
{
    global $db_hostname;
    global $db_username;
    global $db_password;
    global $db_database;
    $link = mysql_connect($db_hostname,$db_username,$db_password);
    if ($link)
    {
        $result = mysql_select_db($db_database);
        if ($result)
        {
            mysql_query("SET NAMES 'utf8'");
            return $link;
        }
    }
    // if any of the tasks related to opening the db fails
    setup_page_redirect();
}

function gen_passwd_hash($password)
{
    global $db_salt;
    return sha1($db_salt.md5($password));
}

function printHeader()
{

    print<<<HTML
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="master.css" />
        <title></title>
    </head>
    <body>
    <div class = "header">
        <h2>&nbsp; MLHTS Record System</h2>
    </div>
    <div class = "leftColumn">
    <table>
        <tr><td><a href="add-new-user.php">Add New User</a></td></tr>
        <tr><td><a href="add-new-student.php">Add New Student</a></td></tr>
        <tr><td><a href="process-payment.php">Process Payment</a></td></tr>
    </table>
    </div>
    <div class = "rightColumn">
HTML;
}

function addLogEntry($table,$changeDescription)
{
    $link=connect_db();
    $query = "INSERT INTO `Log` (`Time`,`Table`,`UserId`,`ChangeDescription`)
                    VALUES ('".date('Y-m-d H:i:s')."',
                            '".$table."',
                            '".getSessionUser()."',
                            '".$changeDescription."');";
    mysql_query($query,$link);
}

/**
 *
 * @return accesslevel - Returns an integer that represents the access level of the user
 *     Link to Privelegde table to get text description of access level from database.
 * @author - Ben Petersen
 * @version - 1.0
 */

function GetCurrentUserAccessLevel()
{

    try
    {
        $currentUser = GetSessionUser();
        $link = connect_db();
        $query = "SELECT PrivilegeLevel FROM `User` where `UserId`= '$currentUser'";
        $result = mysql_query($query, $link);
        if($result===false)
        {
            return false;
        }
        else
        {
            $accesslevel = mysql_fetch_array($result,MYSQL_BOTH);
            return $accesslevel['PrivilegeLevel'];
        }
    }
    catch (Exception $exception)
    {
	$_SESSION['error']=$exception->getMessage();
	return false;
    }
}

/**
 *
 */
function GetGradeFromPercent($percent)
{
    if ($percent >= 70)
    {
        return "A";
    } else if ($percent >= 65)
    {
        return "A-";
    } else if ($percent >= 60)
    {
        return "B+";
    } else if ($percent >= 55)
    {
        return "B";
    } else if ($percent >= 50)
    {
        return "C+";
    } else if ($percent >= 45)
    {
        return "C";
    } else if ($percent >= 40)
    {
        return "D";
    } else {
        return "F";
    }
}

/**
 * Convenience function for getting the text name of a given integer privilege level.
 * @return GroupName - Returns the group name of the given privilege number passed in the parameter
 *
 * @author - Tyler Bradovich
 * @version - 1.0
 */
function GetGroupNameFromPrivLevel($privLevel)
{
    try
    {
        $link = connect_db();
        $query = "SELECT GroupName
					FROM Privilege
					WHERE PrivilegeLevel= '$privLevel'";
        $result = mysql_query($query, $link);
        if($result===false)
        {
            return false;
        }
        else
        {
            $accesslevel = mysql_fetch_array($result,MYSQL_BOTH);
            return $accesslevel['GroupName'];
        }
    }
    catch (Exception $exception)
    {
	$_SESSION['error']=$exception->getMessage();
	return false;
    }
}

/**
 * Convenience function for changing the date to a format the database understands.
 * @return formatted date - Returns the formatted date as a string.
 *
 * @author - Ben Petersen
 * @version - 1.0
 */
function ParseDateValues($thisDate)
{
	list($day, $month, $year) = split ("-", $thisDate);

	return $year."-".$month."-".$day;
}

/**
 * Convenience function for changing the date from a format the database understands to the local format.
 * @return formatted date - Returns the formatted date as a string.
 *
 * @author - Ben Petersen
 * @version - 1.0
 */
function DisplayDate($oldDate)
{
	list($year, $month, $day) = split ("-", $oldDate);
	return $day."-".$month."-".$year;
}
?>
